{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "from common import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "RESULT_JSON = \"/Users/law/repos/viper/results/all_ops/all_ops_viper_versions.json\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "FIXTURES = [VIPER, VIPER_DRAM, VIPER_PMEM, VIPER_UNALIGNED]\n",
    "BM_TYPES = ['insert', 'get']\n",
    "\n",
    "BMS = get_all_runs(RESULT_JSON)\n",
    "\n",
    "RUNS = defaultdict(list)\n",
    "\n",
    "for bm in BMS:\n",
    "    if 'median' not in bm['name']: continue\n",
    "    for (fixture, _) in FIXTURES:\n",
    "        bm_type = get_bm_type(bm['name'])\n",
    "        if fixture in bm['name']:\n",
    "            RUNS[bm_type].append((fixture, bm))\n",
    "            break\n",
    "        \n",
    "for _, run in RUNS.items():\n",
    "    run.sort(key=lambda x: x[0], reverse=True) \n",
    "\n",
    "# pprint(RUNS)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(1, 1, figsize=(4, 2.5))\n",
    "\n",
    "# Set position of bar on X axis\n",
    "bar_width = 0.20\n",
    "bar_diff = 0.02\n",
    "r1 = np.arange(3)\n",
    "r2 = [x + bar_width + bar_diff for x in r1]\n",
    "r3 = [x + bar_width + bar_diff for x in r2]\n",
    "r4 = [x + bar_width + bar_diff for x in r3]\n",
    "\n",
    "Rs = [r1, r2, r3, r4]\n",
    "\n",
    "plt.rcParams['hatch.linewidth'] = 3\n",
    "\n",
    "viper_insert = RUNS['insert'][0][1]['items_per_second'] / MILLION\n",
    "viper_get    = RUNS['get'][0][1]['items_per_second'] / MILLION\n",
    "\n",
    "for i, (f, bm) in enumerate(RUNS['insert']):\n",
    "    color = STYLES[f].color\n",
    "    hatch = '/' if f == VIPER_UNALIGNED[0] else ''\n",
    "    x_pos = Rs[i][0]\n",
    "    tp = bm['items_per_second'] / MILLION\n",
    "    ax.bar(x_pos, tp, bar_width, label=f, color=color, \n",
    "           hatch=hatch, edgecolor='white')\n",
    "    diff = int(((tp - viper_insert) / viper_insert) * 100)\n",
    "    if (diff != 0):\n",
    "        diff_str = f\"{'+' if diff > 0 else '–'}{abs(diff)}%\"\n",
    "        ax.text(x_pos, tp + 2, diff_str, rotation=90, fontsize=16, \n",
    "                va='bottom', ha='center')\n",
    "\n",
    "for i, (f, bm) in enumerate(RUNS['get']):\n",
    "    color = STYLES[f].color\n",
    "    hatch = '/' if f == VIPER_UNALIGNED[0] else ''\n",
    "    x_pos = Rs[i][1]\n",
    "    tp = bm['items_per_second'] / MILLION\n",
    "    ax.bar(x_pos, tp, bar_width, label=f, color=color, \n",
    "           hatch=hatch, edgecolor='white')\n",
    "    diff = int(((tp - viper_get) / viper_get) * 100)\n",
    "    if (f != VIPER[0]):\n",
    "        diff_str = f\"{'+' if diff > 0 else '–'}{abs(diff)}%\"\n",
    "        y_pos = tp + 2\n",
    "        c = 'black'\n",
    "        if f == VIPER_DRAM[0]: \n",
    "            y_pos = tp - 21\n",
    "            c= 'white'\n",
    "        ax.text(x_pos, y_pos, diff_str, rotation=90, fontsize=16, \n",
    "                va='bottom', ha='center', color=c)\n",
    "\n",
    "\n",
    "# ax.set_xticks([r + (1.5 * bar_width) for r in range(2)])\n",
    "ax.set_xticks([(r2[0] + r3[0]) / 2, (r2[1] + r3[1]) / 2])\n",
    "ax.set_axisbelow(True)\n",
    "ax.grid(axis='y', which='major')\n",
    "for tick in ax.yaxis.get_major_ticks():\n",
    "    tick.label.set_fontsize(18)\n",
    "\n",
    "\n",
    "ax.set_ylabel(\"Throughput (Mops/s)\", fontsize=18)\n",
    "ax.yaxis.set_label_coords(-0.15, 0.55)\n",
    "ax.set_xticklabels(['PUT', 'GET'], fontsize=20)\n",
    "\n",
    "fig.legend(loc='upper center', bbox_to_anchor=(0.55, 1.2), \n",
    "           labels=['Viper', 'Unaligned', 'PMem', 'DRAM'],\n",
    "           ncol=2, frameon=False, fontsize=18, \n",
    "           columnspacing=0.4, handletextpad=0.2, borderpad=0.1, \n",
    "           labelspacing=0.1, handlelength=1.8)\n",
    "\n",
    "hide_border(ax)\n",
    "plt.tight_layout()\n",
    "fig.savefig('charts/viper_versions.pdf', bbox_inches='tight')\n",
    "fig.savefig('charts/viper_versions.svg', bbox_inches='tight')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
